<?php
//include ('pe['host_root']/hook/wxlogin.hook.php');
//$_SESSION['user_id'] = 59;
//$_SESSION['user_vipcard_number'] = "";
//$_SESSION['user_moneycard_number'] = "";
//$_SESSION['user_wxid'] = "o6PzEwuIC25N2V8pLJk8P_iIu1oc";
$_SESSION['page'] = "personal_center";
if (!isset($_SESSION['user_id'])) {
    pe_goto('hotel.php?mod=login&act=wx_login');
}

switch ($act) {
    //#####################@ 修改分类 @#####################//


    case 'open_vip':
        //VIP开通条件
        $phone = $_g_phone;
        $user_id = $_SESSION['user_id'];
        $db->pe_update('user',array('user_id'=>$user_id),array('user_phone'=>$phone));
        $_SESSION['open_money'] = 0.01;
        $_SESSION['self_real_price'] = $_SESSION['open_money'];
        Header("Location: http://www.ktxwxgy.com/example/openpay.php");
        break;

    case 'indent_detail':
        //VIP开通条件
        $order = $db->pe_select('order',array('order_id'=>$_g_order_id));
        $order['day_num'] = diffBetweenTwoDays(date('Y/m/d',$order['order_startdate']),date('Y/m/d',$order['order_enddate']));

        include(pe_tpl('indent_detail.html'));
        break;


    case 'delete_ajax':
        $db->pe_update('order',array('order_id'=>$_g_order_id),array('order_delete'=>1));
        ob_clean();
        echo "111111";
        break;

    case 'detail_ajax':
        $order = $db->pe_select('order', array("order_id" => $_GET['order_id']));
        //预约人微信名
        $user = $db->pe_select('user', array('user_wxid' => $order['order_wxid']), "user_wxname");
        //判断是支付类型
        switch ($order['order_payway']) {

            case'1':
                $order['order_payway'] = "线下支付";
                break;

            case'2':
                $order['order_payway'] = "储蓄卡支付";
                break;

            case'3':
                $order['order_payway'] = "微信支付";
                break;

            default:
                $order['order_payway'] = "未支付";
                break;
        }
        $order['order_wxid'] = $user['user_wxname'];
        $order['order_have_bre'] = $order['order_have_bre'] == "0" ? "否": "是";
        $order['night_num'] = diffBetweenTwoDays(date('Y/m/d',$order['order_startdate']),date('Y/m/d',$order['order_enddate']));
        $order['order_enddate'] = date('Y-m-d',$order['order_enddate']);

        ob_clean();
        echo json_encode($order);
        break;


    case 'open_success':

        $user_id = $_SESSION['user_id'];
        $user = $db->pe_select('user', array('user_id' => $user_id));
        $vipcard_number = rand(10000000, 100000000);
        $db->pe_update('user', array('user_id' => $user_id), array('user_vipcard_number' => $vipcard_number));
        //找到会员卡分类
        $vip_cate = $db->pe_select('vipcategory', array('vipcategory_id' => 3));

        $db->pe_insert('vipcard', array('vipcard_number' => $vipcard_number, 'vipcard_category_id' => 3, 'vipcard_uid' => $user_id, 'vipcard_sale' => $vip_cate['vipcategory_sale'],'vipcard_name'=>$vip_cate['vipcategory_name']));
        $_SESSION['user_vipcard_number'] = $vipcard_number;
        pe_success('开通会员成功','hotel.php?mod=personal_center');
        break;

    case 'charge_moneycard':
//        $_SESSION['charge_money'] = $_g_charge_money;
        $_SESSION['charge_money'] = 0.01;
        $_SESSION['self_real_price'] = $_SESSION['charge_money'];
        Header("Location: http://www.ktxwxgy.com/example/chargepay.php");
        break;

    case 'charge_success':
        $user_id = $_SESSION['user_id'];
        $moneycard = $db->pe_select('moneycard', array('moneycard_uid' => $user_id));
        $money = $moneycard['moneycard_money'] + $_SESSION['self_real_price'];

        $charge_money = intval($_SESSION['self_real_price']);

        $db->pe_update('moneycard', array('moneycard_id' => $moneycard['moneycard_id']), array('moneycard_money' => $money));
        $db->pe_insert('moneycard_record', array('record_money' => $_SESSION['self_real_price'], 'record_card_num' => $moneycard['moneycard_number'], 'record_type' => 1, 'record_time' => time(), 'record_uid' => $_SESSION['user_id']));

        pe_success('充值成功','hotel.php?mod=personal_center');
        break;


    case 'open_moneycard':
        $wxid = $_SESSION['user_wxid'];
        $user_id = $_SESSION['user_id'];
        $user_info = $db->pe_select('user', array('user_wxid' => 'zayn'));
        $user_moneycard_number = getMillisecond() . rand(1, 100);
        $db->pe_update('user', array('user_id' => $user_id), array('user_moneycard_number' => $user_moneycard_number));
        $db->pe_insert('moneycard', array('moneycard_wxid' => $wxid, 'moneycard_number' => $user_moneycard_number, 'moneycard_money' => 0, 'moneycard_uid' => $user_id));
        $_SESSION['user_moneycard_number'] = $user_moneycard_number;
        pe_success('储蓄卡开通成功','hotel.php?mod=personal_center');
        break;

    case 'sub_refund':
        $db->pe_update('order', array('order_id' => $_p_order_id), array('order_ostate' => 4, 'order_pushreason' => $_p_because));

        pe_success('申请退款成功','hotel.php?mod=personal_center');
        break;


    //#####################@ 分类列表 @#####################//
    default :
//        mp($_SESSION);
        $user_id = $_SESSION['user_id'];
        //会员升级条件及开通条件
        $setting = $db->index('setting_key')->pe_selectall('setting');
        //用户信息
        $user_info = $db->pe_select('user', array('user_id' => $user_id));
        //用户的红包

        $sql = 'select * from wxgg_redpack as r where   r.redpack_userid =' . $user_id . ' and redpack_state != 1';
        $user_redpack = $db->sql_selectall($sql);

        foreach ($user_redpack as $k => $v) {
            if ($v['redpack_enddate'] > time()) {
                $user_redpack[$k]['is_effective'] = 1;
            } else {
                $user_redpack[$k]['is_effective'] = 0;
            }
            $house = $db->pe_select('house', array('house_id' => $v['redpack_houseid']));
            $user_redpack[$k]['redpack_enddate'] = date('Y-m-d', $v['redpack_enddate']);
            $user_redpack[$k]['redpack_houseid'] = $house['house_name'];
        }

//        mp($user_redpack);exit;
        //判断是否有储蓄卡
        if (!empty($_SESSION['user_moneycard_number'])) {
            $user_moneycard_number = 1;
        } else {
            $user_moneycard_number = 0;
        }

        //判断是否有会员卡
        if (!empty($_SESSION['user_vipcard_number'])) {
            $user_vipcard_number = 1;
        } else {
            $user_vipcard_number = 0;
        }

        //找到会员卡以及确定会员卡等级
        if ($user_vipcard_number == 1) {
            $user_vip = $db->pe_select('vipcard', array('vipcard_uid' => $user_id));
        }

        //找到储蓄卡
        if ($user_moneycard_number == 1) {
            $user_moneycard = $db->pe_select('moneycard', array('moneycard_uid' => $user_id));
        }

//        echo "储蓄卡".$user_moneycard_number;
//        echo "会员卡".$user_vipcard_number;
//        exit;
        $user_wxid = $_SESSION['user_wxid'];
        //用户订单分类
        $order_sql = 'select order_ostate,group_concat(o.order_id) as all_order,group_concat(o.order_houseid) as all_house  from wxgg_order as o where order_wxid = ' . "'$user_wxid'" . ' and order_delete!=1 group by order_ostate';
        ob_clean();
        $user_order = $db->sql_selectall($order_sql);

        //消费记录
        $cost_record = $db->pe_selectall('moneycard_record', array('record_uid' => $user_id, 'record_type' => 2));
        //充值记录
        $charge_record = $db->pe_selectall('moneycard_record', array('record_uid' => $user_id, 'record_type' => 1));

        foreach ($user_order as $k => $v) {
            $user_order[$k]['all_order'] = explode(',', $v['all_order']);
            $user_order[$k]['all_house'] = explode(',', $v['all_house']);

            foreach ($user_order[$k]['all_order'] as $i => $j) {
                $order = $db->pe_select('order', array('order_id' => $j));
                $house = $db->pe_select('house', array('house_id' => $user_order[$k]['all_house'][$i]), 'house_name,house_img');

                $user_order[$k]['order'][$i] = $order;
                $user_order[$k]['order'][$i]['house_name'] = $house['house_name'];
                $user_order[$k]['order'][$i]['house_img'] = $house['house_img'];

            }

        }

        //代付款订单总数
        $wait_sql = 'select * from wxgg_order where order_userid = ' . $user_id . ' and order_ostate = 0 and order_delete != 1 ';
        $wait_order = count($db->sql_selectall($wait_sql));
//        $wait_order = count($db->pe_selectall('order',array('order_userid'=>$user_id,'order_ostate'=>0,'order_delete')));
        //待使用订单总数
        $wait_use_order_sql = 'select * from wxgg_order where order_userid = ' . $user_id . ' and order_ostate = 1 and order_delete != 1 ';
        $wait_use_order = count($db->sql_selectall($wait_use_order_sql));
//        $wait_use_order = count($db->pe_selectall('order',array('order_userid'=>$user_id,'order_ostate'=>1)));
        //已使用订单
        $use_order_sql = 'select * from wxgg_order where order_userid = ' . $user_id . ' and order_ostate = 2 and order_delete != 1 ';
        $use_order = count($db->sql_selectall($use_order_sql));
//        $use_order = count($db->pe_selectall('order',array('order_userid'=>$user_id,'order_ostate'=>2)));
        //退款，售后订单
        $refund_order_sql = 'select * from wxgg_order where order_userid = ' . $user_id . ' and (order_ostate = 4 or order_ostate = 5) and order_delete != 1 ';
        $refund_order = count($db->sql_selectall($refund_order_sql));
//        $refund_order_sql = 'select * from wxgg_order where order_ostate = 4 or order_ostate = 5';
//        $refund_order = count($db->sql_selectall($refund_order_sql));

        include(pe_tpl('personal_center.html'));
        break;
}


